package com.scm.servlet.finance;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.scm.model.Pomain;
import com.scm.page.Page;
import com.scm.service.PomainService;
/**
 * 付款登记首页
 * @author Surrik_Zhang
 */
@WebServlet("/main/finance/paymentIndex")
public class PaymentShowServlet extends HttpServlet{
	private static final long serialVersionUID = -6567905365574919324L;
	private static final Logger LOGGER = Logger.getLogger(PaymentShowServlet.class);
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//获取当前页码参数
		String currentPageStr = req.getParameter("currentPage");
		String payType = req.getParameter("payType");
		if("货到付款".equals(payType)) {
			payType="1";
		}else if("款到发货".equals(payType)) {
			payType="2";
		}else if("预付款到发货".equals(payType)) {
			payType="3";
		}
		//默认当前页码为1
		int currentPage = 1;
		try{
			if(currentPageStr != null && currentPageStr.matches("\\d+")){
				currentPage = Integer.parseInt(currentPageStr);
			}
		}catch(NumberFormatException e){
			//参数不是数字或者为空的时候，默认为第一页
			currentPage = 1;
		}
		PrintWriter out = resp.getWriter();
		Map<String,String> queryCondition = new HashMap<String,String>();
		queryCondition.put("type", "1");
		queryCondition.put("payType",payType);
		queryCondition.put("poid", req.getParameter("poid"));
		queryCondition.put("name", req.getParameter("name"));
		queryCondition.put("startDate", req.getParameter("startDate"));
		queryCondition.put("endDate", req.getParameter("endDate"));
		//打印查询条件
		LOGGER.info("[采购单分页查询条件]-->"+queryCondition);
		
		try {
			Page<Pomain> page = new PomainService().selectPage(currentPage, queryCondition);
			req.setAttribute("page", page);
			out.print(queryCondition.get("payType"));
			
			req.getRequestDispatcher("/main/finance/payment_layer.jsp").forward(req, resp);
		} catch (Exception e) {
			LOGGER.error("付款登记首页异常",e);
			e.printStackTrace();
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
}
